package managedbean.CI.Position; 
 
import entity.Role;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB; 
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean; 
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent; 
import session.stateless.PositionSessionBeanLocal;
import session.stateless.RoleSessionBeanLocal;
 
@ManagedBean(name="positionManagerBean")
@RequestScoped
public class PositionManagerBean implements Serializable { 
    @EJB 
    private PositionSessionBeanLocal positionSessionBeanLocal; 
    
    @EJB 
    private RoleSessionBeanLocal roleSessionBeanLocal; 
     
    private String positionName; 
    private List<Role> roles;
    private Role[] selectedRoles;
    private String statusMessage;
    private long positionId;    
    
    public String getPositionName() { 
        return positionName; 
    } 
    
    public void setPositionName(String positionName) { 
        this.positionName = positionName; 
    }

    public List<Role> getRoles() {
        return roleSessionBeanLocal.getRoles();
    }
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Role[] getSelectedRoles() {
        return selectedRoles;
    }

    public void setSelectedRoles(Role[] selectedRoles) {
        this.selectedRoles = selectedRoles;
    }
    
    public String getStatusMessage() { 
        return statusMessage; 
    } 
    public void setStatusMessage(String statusMessage) { 
        this.statusMessage = statusMessage; 
    }
    
    public void saveNewPosition(ActionEvent event) 
    { 
        String temp_PositionDescription = String.valueOf(positionName);
        List<Role> temp_SelectedRoles = new ArrayList<Role>();
        for (Role role : this.getSelectedRoles()){
            temp_SelectedRoles.add(role);
        }               
        try 
        { 
            System.out.println("test2"+temp_PositionDescription+"    "+temp_SelectedRoles.size());
            positionId = positionSessionBeanLocal.createPosition(temp_PositionDescription, temp_SelectedRoles);
            statusMessage = "New Position Saved Successfully"; 
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Add New Position Result: " + statusMessage + " (New Position ID is " + positionId + ")", "")); 
        } 
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
    }    
}
    

